package postgres

import (
	"context"
	"gorm.io/gorm"
	"manager/errors"
	"manager/model"
	"manager/model/entity"
	"manager/utils"
)

var Lose = &lose{}

type lose struct{}

// Create 创建
func (a *lose) Create(ctx context.Context, m *entity.Lose) (int64, error) {
	err := GetDB(ctx).Create(m).Error
	return m.Id, err
}

// Find 查找详情
func (a *lose) Find(ctx context.Context, in *model.LoseInfoRequest) (*entity.Lose, error) {
	e := &entity.Lose{}

	q := GetDB(ctx).Model(&entity.Lose{})

	if in.Id > 0 {
		err := q.First(&e, in.Id).Error
		return e, err
	}

	count := 0

	if in.EbikeId != nil {

		q = q.Where("ebike_id = ?", in.EbikeId)

		count++
	}

	if in.UserId != nil {

		q = q.Where("user_id = ?", in.UserId)

		count++
	}

	if in.Addr != nil {

		q = q.Where("addr like ?", in.Addr)

		count++
	}

	if in.LoseTime != nil {

		q = q.Where("lose_time = ?", in.LoseTime)

		count++
	}

	if in.Status != nil {

		q = q.Where("status = ?", in.Status)

		count++
	}

	if in.LockStatus != nil {

		q = q.Where("lock_status = ?", in.LockStatus)

		count++
	}

	if in.Result != nil {

		q = q.Where("result like ?", in.Result)

		count++
	}

	if in.SolverId != nil {

		q = q.Where("solver_id = ?", in.SolverId)

		count++
	}

	if in.Desc != nil {

		q = q.Where("desc like ?", in.Desc)

		count++
	}

	if in.UpdatedAt != nil {

		q = q.Where("updated_at = ?", in.UpdatedAt)

		count++
	}

	if count == 0 {
		return e, errors.New("condition illegal")
	}

	err := q.Order("created_at desc").First(&e).Error
	return e, err
}

// Update 更新
func (a *lose) Update(ctx context.Context, id int64, dict map[string]interface{}) error {
	return GetDB(ctx).Model(&entity.Lose{}).Where("id = ?", id).Updates(dict).Error
}

// Delete 删除
func (a *lose) Delete(ctx context.Context, id int64) error {
	return GetDB(ctx).Delete(&entity.Lose{}, id).Error
}

// List 列表查询
func (a *lose) List(ctx context.Context, in *model.LoseListRequest) (int, []*entity.Lose, error) {
	var (
		q     = GetDB(ctx).Model(&entity.Lose{})
		err   error
		total int64
		loses []*entity.Lose
	)

	if in.EbikeId != nil {

		q = q.Where("ebike_id = ?", in.EbikeId)

	}

	if in.UserId != nil {

		q = q.Where("user_id = ?", in.UserId)

	}

	if in.LoseType != nil {
		q = q.Where("lose_type = ?", in.LoseType)
	}

	if in.Addr != nil {

		q = q.Where("addr like ?", utils.SqlLike(in.Addr))

	}

	if in.LoseTime != nil {

		q = q.Where("lose_time = ?", in.LoseTime)

	}

	if in.Status != nil {

		q = q.Where("status = ?", in.Status)

	}

	if in.LockStatus != nil {

		q = q.Where("lock_status = ?", in.LockStatus)

	}

	if in.Result != nil {

		q = q.Where("result like ?", in.Result)

	}

	if in.SolverId != nil {

		q = q.Where("solver_id = ?", in.SolverId)

	}

	if in.Desc != nil {

		q = q.Where("desc like ?", in.Desc)

	}

	if in.EbikeTypeId != nil {
		q = q.Where("ebike_type_id = ?", in.EbikeTypeId)
	}

	if in.UpdatedAt != nil {

		q = q.Where("updated_at = ?", in.UpdatedAt)

	}

	// 搜索时间
	if in.SearchTime != nil && len(*in.SearchTime) > 1 {
		q = q.Where("lose_time between ? and ?", (*in.SearchTime)[0], (*in.SearchTime)[1])
	}

	if len(in.TimeZone) == 2 {
		q = q.Where("lose_time between ? and ?", in.TimeZone[0], in.TimeZone[1])
	}

	if err = q.Count(&total).Error; err != nil {
		return 0, nil, err
	}
	if err = q.Order("created_at desc").Limit(in.Size).Offset((in.Index - 1) * in.Size).Find(&loses).Error; err != nil {
		return 0, nil, err
	}
	return int(total), loses, nil
}

// ExecTransaction db事务执行
func (a *lose) ExecTransaction(ctx context.Context, callback func(ctx context.Context) error) error {
	return GetDB(ctx).Transaction(func(tx *gorm.DB) error {
		ctx = context.WithValue(ctx, DBCONTEXTKEY, tx)
		return callback(ctx)
	})
}
